package com.price.sample.ui

import android.os.Bundle
import android.view.View.OnClickListener
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.price.common.base.BaseActivity
import com.price.common.extend.addStatusBarHeightToPaddingTop
import com.price.common.extend.toast
import com.price.design.doOnDismiss
import com.price.design.multiChoiceSelector
import com.price.design.selector
import com.price.design.singleChoiceSelector
import com.price.sample.SimpleAdapter
import com.zp.common.R

class DialogsActivity : BaseActivity() {


    override fun getViewId() =R.layout.activity_recycler_view

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(R.string.dialogs)
        setContentView(R.layout.activity_recycler_view)
        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        recyclerView.addStatusBarHeightToPaddingTop()
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
        val adapter = SimpleAdapter()
        recyclerView.adapter = adapter
        adapter.submitList(listOf(
            R.string.selector,
            R.string.single_choice_selector,
            R.string.multi_choice_selector,
        ))
        adapter.doOnClick= OnClickListener { v ->
            when (v?.tag as Int) {
                R.string.selector -> checkCountry()
                R.string.single_choice_selector -> selectCountry()
                R.string.multi_choice_selector -> selectFoods()
            }
        }
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    private fun checkCountry() {
        val countries = listOf("China", "Russia", "USA", "Australia")
        selector(countries, "Where are you from?") { _, i ->
            toast("So you're living in ${countries[i]}, right?")
        }
    }

    private val countries = listOf("China", "Russia", "USA", "Australia")
    private var checkedCountry = "China"
    private fun selectCountry() {
        val checkedIndex = countries.indexOfFirst { it == checkedCountry }
        singleChoiceSelector(countries, checkedIndex, "Where are you from?") { dialog, i ->
            checkedCountry = countries[i]
            toast("You're living in ${checkedCountry}.")
            dialog.dismiss()
        }
    }

    private val foods = listOf("Apple", "Banana", "Pear", "Peach")
    private val checkedItems = BooleanArray(foods.size)
    private fun selectFoods() {
        multiChoiceSelector(foods, checkedItems, "What do you want to eat?") { _, i, isChecked ->
            checkedItems[i] = isChecked
        }.doOnDismiss {
            toast("So you want to eat ${checkedItems.filter { it }.size} foods.")
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
}